<?php
class ChartAction extends CommonAction {
	public function index() {
		$this->display();
	}
	public function data() {
		$method = $_POST;
		$userinfo = Session::get('userinfo');
		$uObj = new UserModel();
		$dObj = new DepartmentModel();
		switch($method['do']){
			case 'groupid':
				switch($userinfo['type_id']){
					case 1:
						foreach($dObj->select() as $v){
							if($v['did'] == 1) {
								unset($v);
							}
							else{
								$user[] = $v;
							}
						}
						//$user[] = array('id'=>0,'name'=>'全部');
						echo json_encode($user);
					break;
					case 2:
						$map['did'] = $userinfo['did'];
						foreach($dObj->where($map)->select() as $v){
							$user[] = $v;
						}
						echo json_encode($user);
					break;
				}
			break;
			case 'userid':
				$uObj->field('id,account,nickname as name,did');

				foreach($dObj->select() as $v)
				{
					$dlist[$v['did']] = $v;
				}
				switch($userinfo['type_id']){
					case 1:
						foreach($uObj->select() as $v){
							if($v['did'] <> 1){
								$v['name'] = $dlist[$v['did']]['name'] . '-' . $v['name'];
								$user[] = $v;
							}
						}
						echo json_encode($user);
					break;
					case 2:
						$map['did'] = $userinfo['did'];
						foreach($uObj->where($map)->select() as $v){
							$user[] = $v;
						}
						echo json_encode($user);
					break;
				}
			break;
			case 'leftid':
				$array = array();
				$array[] = array('id' => 1 ,'text' => '各组统计','leaf' => true);
				//$array[] = array('id' => 'all' ,'text'=> '全组统计','leaf' => true);
				echo json_encode($array);
			break;
		}
	}
	public function search() {
		$method = $_POST;
		$map = array();
		$zixunObj = D('zixun');
		$userObj = D('user');
		$userinfo = Session::get('userinfo');
		switch($method['dateselect']){
			case 1:
				$starttime =  (strtotime($method['startdate']) - 1);
				$endtime =  (strtotime($method['enddate']) + 1);
				$map['date'] =  array(array('gt',$starttime),array('lt',$endtime));
				break;
			case 2:
				$startmonth = strtotime($method['startmonth']) -1;
				$endmonth = strtotime($method['endmonth']);
				$counday = idate('t',$endmonth);
				$endmonth = mktime(24,0,0,date('m',$endmonth),$counday,date('Y',$endmonth)) -1;
				$map['date'] =  array(array('gt',$startmonth),array('lt',$endmonth));
			break;
			case 3:
				$map['date'] = strtotime($method['datemonth']);
				$counday = idate('t',$map['date']);
				$s = mktime(0,0,0,date('m',$map['date']),1,date('Y',$map['date'])) -1;
				$e = mktime(24,0,0,date('m',$map['date']),$counday,date('Y',$map['date'])) -1;
				$map['date'] =  array(array('gt',$s),array('lt',$e));
			break;
		}

		switch($userinfo['type_id']){
			case 1:
				if($method['did'] <> 0 )
					$usermap['did'] = $method['did'];
			break;
			case 2:
				$usermap['did'] = $userinfo['did'];
			break;
		}
		foreach($userObj->where($usermap)->select() as $v){
			$user[$v['id']] = $v['nickname'];
			$ids[] = $v['id'];
		}
		$map['uid'] = array('in',$ids);
		$data = $zixunObj->where($map)->select();
		switch($method['type']){
			case 1:$this->Pie($user,$data);break;
			case 2:$this->Column($user,$data);break;
			case 3:$this->MSLine($user,$data);break;
		}
	}
	private function Pie($user,$data){

		foreach($data as $v){
			$d['yuyue'][$v['uid']][] = intval($v['yuyue']);
			$d['jiuzhen'][$v['uid']][] = intval($v['jiuzhen']);
			$uid[$v['uid']] = $v['uid'];
		}
		foreach($user as $k=>$v){
			if(!in_array($k,$uid))
			{
				$d['yuyue'][$k][] = 0;
				$d['jiuzhen'][$k][] = 0;
			}
		}
		foreach($d as $k =>$v){
			$arrs[$k][] = '<graph showNames="1" decimalPrecision="0"  baseFontSize="13">';
			ksort($d[$k]);
			foreach($d[$k] as $kk => $vv){
				$value = array_sum($vv);
				$arrs[$k][] = "<set name='{$user[$kk]}' value='{$value}'/>";
			}
			$arrs[$k][] = '</graph>';
		}
		 $this->success('发送成功',array('yuyue'=>implode('',$arrs['yuyue']),'jiuzhen'=>implode('',$arrs['jiuzhen'])));
	}
	private function Column($user,$data){
		foreach($data as $v){
			$d['yuyue'][$v['uid']][] = intval($v['yuyue']);
			$d['jiuzhen'][$v['uid']][] = intval($v['jiuzhen']);
			$d['youxiao'][$v['uid']][] = intval($v['youxiao']);
			$uid[$v['uid']] = $v['uid'];
		}
		foreach($user as $k=>$v){
			if(!in_array($k,$uid))
			{
				$d['yuyue'][$k][] = 0;
				$d['jiuzhen'][$k][] = 0;
				$d['youxiao'][$k][] = 0;
			}
		}
		$seriesName = array(
			'yuyue'=> array('name'=>'预约','color'=>'D71F8D'),
			'jiuzhen'=>array('name'=>'就诊','color'=>'28E072'),
			'youxiao'=>array('name'=>'有效咨询','color'=>'FF9000')
		);

		foreach($d as $k =>$v){
			$arr[] = "<dataset seriesname='{$seriesName[$k]['name']}' color='{$seriesName[$k]['color']}'>";
				ksort($d[$k]);
				foreach($d[$k] as $kk => $vv){
					$value = array_sum($vv);
					$arr[] = "<set value='{$value}'/>";
				}
			$arr[] = '</dataset>';
		}
		$method = $_POST;
		switch($method['dateselect']){
			case 1:
				$starttime =  strtotime($method['startdate']);
				$endtime =  strtotime($method['enddate']);
				$intro = "数据至".date('Y年m月d日',$starttime).'-'.date('Y年m月d日',$endtime);
				break;
			case 2:
				$startmonth = strtotime($method['startmonth']);
				$endmonth = strtotime($method['endmonth']);
				$counday = idate('t',$endmonth);
				$endmonth = mktime(24,0,0,date('m',$endmonth),$counday,date('Y',$endmonth)) -1;
				$intro = "数据至".date('Y年m月',$startmonth).'-'.date('Y年m月',$endmonth);
			break;
			case 3:
				$map['date'] = strtotime($method['datemonth']);
				$intro = '数据至'.date('Y年m月',$map['date']);
			break;
		}
		$arr[] = '<categories font="Arial" fontSize="11" fontColor="000000">';
		foreach($user as $v){
			$arr[] = "<category name='{$v}'/>";
		}
		$arr[] = '</categories>';
		//print_r($arr);
		$info = '<graph xaxisname="" baseFontSize="13" yaxisname="@" hovercapbg="DEDEBE" hovercapborder="889E6D" rotateNames="0" yAxisMaxValue="100" numdivlines="9" divLineColor="CCCCCC" divLineAlpha="80" decimalPrecision="0" showAlternateHGridColor="1" AlternateHGridAlpha="30" AlternateHGridColor="CCCCCC" caption="各组员预约就诊情况图(@代表人数)" subcaption="'.$intro.'">'.implode('',$arr).'</graph>';
		$this->success('发送成功',$info);
	}
	private function MSLine($user,$data){
		$method = $_POST;
		switch($method['dateselect']){
			case 1:
				$starttime =  strtotime($method['startdate']);
				$endtime =  strtotime($method['enddate']);

				function datea($starttime,$endtime)
				{
					$starttime = getdate($starttime);
					$endtime = getdate($endtime);
					$datenum = $endtime['yday'] - $starttime['yday'];
					$datenum = $datenum < 90 ? $datenum : 90;
					for($i=0;$i<=$datenum;$i++){
						$m = date('Y-m-d', ($starttime[0]+ 86400 * $i));
						$arr[$m] = $m;
					}
					return $arr;
				}

				$arr = datea($starttime,$endtime);
				foreach($data as $v){
					$d['yuyue'][date('Y-m-d',$v['date'])][] = intval($v['yuyue']);
					$d['jiuzhen'][date('Y-m-d',$v['date'])][] = intval($v['jiuzhen']);
					$date[date('Y-m-d',$v['date'])] = date('Y-m-d',$v['date']);
				}
				foreach($arr as $k => $v){
					if(!in_array($k,$date))
					{
						$d['yuyue'][$k][] = 0;
						$d['jiuzhen'][$k][] = 0;
					}
				}
				$intro = "数据至".date('Y年m月d日',$starttime).'-'.date('Y年m月d日',$endtime);
				break;
			case 2:
				$startmonth = strtotime($method['startmonth']);
				$endmonth = strtotime($method['endmonth']);
				function datea($starttime,$endtime)
				{
					$starttime = getdate($starttime);
					$endtime = getdate($endtime);
					$datenum = $endtime[0] - $starttime[0];
					$date = new Date(date('Y-m',$starttime[0]));
					$datenum = intval($date->dateDiff(date('Y-m',$endtime[0]),'M')) + 1;
					$datenum = $datenum < 12 ? $datenum : 12;
					for($i=0;$i<$datenum;$i++){
						$r = ($starttime['mon'] + $i);
						if($r > 12){
							$r = 1 + ($j ++);
							if($j > 12){
								$r = 1 + ($u ++);
							}else
								$y = 1;
						}
						$m = date('Y-m', strtotime(((date('Y',$starttime[0])+$y) .'-'.$r)));
						$arr[$m] = $m;
					}
					return $arr;
				}
				$arr = datea($startmonth,$endmonth);
				foreach($data as $v){
					$d['yuyue'][date('Y-m',$v['date'])][] = intval($v['yuyue']);
					$d['jiuzhen'][date('Y-m',$v['date'])][] = intval($v['jiuzhen']);
					$date[date('Y-m',$v['date'])] = date('Y-m',$v['date']);
				}
				foreach($arr as $k => $v){
					if(!in_array($k,$date))
					{
						$d['yuyue'][$k][] = 0;
						$d['jiuzhen'][$k][] = 0;
					}
				}
				$intro = "数据至".date('Y年m月',$startmonth).'-'.date('Y年m月',$endmonth);
			break;
			case 3:
				$map['date'] = strtotime($method['datemonth']);
				function datea($time){
					$counday = idate('t',$time);
					$s = mktime(0,0,0,date('m',$time),1,date('Y',$time));
					$e = mktime(24,0,0,date('m',$time),$counday,date('Y',$time)) -1;
					for($i=0;$i<$counday;$i++){
						$m = date('Y-m-d',($s + (86400 * $i)));
						$arr[$m] = $m;
					}
					return $arr;
				}
				$arr = datea($map['date']);
				foreach($data as $v){
					$d['yuyue'][date('Y-m-d',$v['date'])][] = intval($v['yuyue']);
					$d['jiuzhen'][date('Y-m-d',$v['date'])][] = intval($v['jiuzhen']);
					$date[date('Y-m-d',$v['date'])] = date('Y-m-d',$v['date']);
				}
				foreach($arr as $k => $v){
					if(!in_array($k,$date))
					{
						$d['yuyue'][$k][] = 0;
						$d['jiuzhen'][$k][] = 0;
					}
				}
				$intro = '数据至'.date('Y年m月',$map['date']);
			break;
		}

		$seriesName = array(
			'yuyue'=> array('name'=>'预约','color'=>'D71F8D'),
			'jiuzhen'=>array('name'=>'就诊','color'=>'28E072')
		);
		foreach($d as $k =>$v){
			$arrs[] = "<dataset seriesName='{$seriesName[$k]['name']}' color='{$seriesName[$k]['color']}' anchorBorderColor='{$seriesName[$k]['color']}' anchorBgColor='{$seriesName[$k]['color']}'>";
			ksort($d[$k]);
			foreach($d[$k] as $kk => $vv){

				$value = array_sum($vv);
				$arrs[] = "<set value='{$value}'/>";
			}
			$arrs[] = '</dataset>';
		}


		$arrs[] = '<categories fontSize="9">';
		foreach($arr as $v){
			$arrs[] = "<category name='{$v}'/>";
		}
		$arrs[] = '</categories>';
		$this->success('发送成功',"<graph baseFontSize='13' caption='预约就诊走势图' subcaption='".$intro."' hovercapbg='FFECAA' hovercapborder='F47E00' formatNumberScale='0' decimalPrecision='0' showvalues='0' numdivlines='3' numVdivlines='0' rotateNames='1'>".implode('',$arrs)."</graph>");
	}
	public function execl(){
		$method = $_POST;
		$yuyueObj = D('yuyue');
		$userObj = D('user');
		$dObj = new DepartmentModel();
		$binzhongObj = D('binzhong');
		$userinfo = Session::get('userinfo');
		$app = new COM("Excel.Application") or die("Did Not Connect");
		$path = dirname(dirname(dirname(__FILE__))) .'\\public\\data\\';
		$pathname = time().'.xls';
		$file = $path.'file/'.$pathname;
		$urlpath = __ROOT__ .'/'.APP_PATH.'/public/data/file/';
		switch($userinfo['type_id']){
			case 1:
				$usermap['did'] = $method['did'];
			break;
			case 2:
				$usermap['did'] = $userinfo['did'];
			break;
		}
		foreach($userObj->field('id,nickname as name')->where($usermap)->select() as $v){
			$user[$v['id']] = $v;
		}
		//print_r($userObj);
		foreach($binzhongObj->select() as $v){
			$binzhong[$v['id']] = $v['name'];
		}
		switch($method['do']){
			case 'count':
				$jobObj = D('job');
				$zixunObj = D('zixun');
				$workdayObj = D('workday');
				$app->WorkBooks->Open($path.'count.xls');
				$worksheet1 = $app->WorkSheets(1);
				$worksheet2 = $app->WorkSheets(2);
				$worksheet3 = $app->WorkSheets(3);
				$worksheet4 = $app->WorkSheets(4);
				$ids = array();
				function datea($time){
					$counday = idate('t',$time);
					$s = mktime(0,0,0,date('m',$time),1,date('Y',$time));
					$e = mktime(24,0,0,date('m',$time),$counday,date('Y',$time)) -1;
					for($i=0;$i<$counday;$i++){
						$m = date('Y-m-d',($s + (86400 * $i)));
						$arr[$m] = $m;
					}
					return $arr;
				}
				$dmap['did'] = $usermap['did'];
				$dp = $dObj->where($dmap)->find();
				$times = getdate(strtotime($method['datemonth']));
				//某某项目组咨询工作简报
				$worksheet1->Cells(1,1)->Value = mb_convert_encoding($dp['name'].'咨询工作简报',"gb2312","UTF-8");
				$worksheet1->Cells(2,2)->Value = $times['mon'];
				$worksheet1->Cells(2,5)->Value = $times['wday'] == 0 ? 7 : $times['wday'];
				$arr = datea(strtotime($method['datemonth']));
				$i= 2;
				ksort($user);
				foreach($user as $k => $v){
					$worksheet2->Cells(1,$i)->Value = mb_convert_encoding($v['name'],"gb2312","UTF-8");
					$ids[$i] = $v['id'];
					$i = $i + 3;
				}
				$map['uid'] = array('in',$ids);
				$map['date'] = strtotime($method['datemonth']);
				$counday = idate('t',$map['date']);
				$s = mktime(0,0,0,date('m',$map['date']),1,date('Y',$map['date'])) -1;
				$e = mktime(24,0,0,date('m',$map['date']),$counday,date('Y',$map['date'])) -1;
				$map['date'] =  array(array('gt',$s),array('lt',$e));
				$uid = array();
				foreach($jobObj->where($map)->select() as $v){
					$job[$v['uid']][] = $v;
					$uid[$v['uid']] = $v['uid'];
					$jobcount[] = $v['jiuzhen'];
				}
				$worksheet1->Cells(2,8)->Value = array_sum($jobcount);
				foreach($user as $k=>$v){
					if(!in_array($k,$uid))
					{
						$job[$k][] = 0;
					}
				}
				ksort($job);
				$i= 6;
				foreach($job as $v){
					$worksheet1->Cells($i,2)->Value = $v[0]['jiuzhen'];
					$i++;
				}

				$date = array();
				foreach($zixunObj->where($map)->select() as $v){
					$date[date('Y-m-d',$v['date'])] = date('Y-m-d',$v['date']);
					$zixun[date('Y-m-d',$v['date'])][$v['uid']] = $v;
				}
				foreach($arr as $k => $v){
					if(!in_array($k,$date))
					{
						$zixun[$k][] = array();
					}
				}
				ksort($zixun);
				$i = 4;
				foreach($zixun as $k => $v){
					foreach($v as $kk => $vv){
						$j = 2;
						if($o = array_search($vv['uid'],$ids)){
							$j = $o == 2 ? 2 : $o;
						}
						if($vv['zixun']) $worksheet2->Cells($i,$j++)->Value = $vv['zixun'];
						if($vv['youxiao']) $worksheet2->Cells($i,$j++)->Value = $vv['youxiao'];
						if($vv['zongyouxiao']) $worksheet2->Cells($i,$j++)->Value = $vv['zongyouxiao'];
					}
					$i++;
				}
				$i = 4;
				foreach($zixun as $k => $v){
					foreach($v as $kk => $vv){
						$j = 2;
						if($o = array_search($vv['uid'],$ids)){
							$j = $o == 2 ? 2 : $o;
						}
						if($vv['yuyue']) $worksheet3->Cells($i,$j++)->Value = $vv['yuyue'];
						if($vv['jiuzhen']) $worksheet3->Cells($i,$j++)->Value = $vv['jiuzhen'];
					}
					$i++;
				}
				$date = array();
				foreach($workdayObj->where($map)->select() as $v){
					$workday[date('Y-m-d',$v['date'])][$v['uid']] = $v;
					$date[date('Y-m-d',$v['date'])] = date('Y-m-d',$v['date']);
				}
				foreach($arr as $k => $v){
					if(!in_array($k,$date))
					{
						$workday[$k][] = array();
					}
				}
				ksort($workday);
				$i = 4;
				$font = array(1=>'早',2=>'中',3=>'晚',4=>'行');
				$fontcolor = array(1=>'3723875',2=>'7301384',3=>'6229504',4=>'19683');//->Interior->Color
				foreach($workday as $k => $v){
					foreach($v as $kk => $vv){
						$j = 2;
						if($o = array_search($vv['uid'],$ids)){
							$j = $o == 2 ? 2 : $o;
						}
						if($vv['type']){
							$worksheet4->Cells($i,$j)->Value = mb_convert_encoding($font[$vv['type']],"gb2312","UTF-8");
							$j++;
						}
					}
					$i++;
				}
				//$worksheet4->Cells(4,16)->Interior->ColorIndex = 45;
				$worksheet2->SaveAs($file);
				$app->WorkBooks->Close();
				unset($app);
				$this->success('发送成功',$urlpath.$pathname);
			break;
			case 'group':
				$app->WorkBooks->Open($path.'group.xls');
				$worksheet = $app->WorkSheets(1);
				$worksheet->Cells(1,1)->Value = mb_convert_encoding('网络预约日报表（'.date('Y-m-d',strtotime($method['dateday'])).'）',"gb2312","UTF-8"); //网络预约日报表（2010-07-25）
				$ids = array();
				foreach($user as $k => $v){
					$ids[] = $v['id'];
				}
				$map['uid'] = array('in',$ids);
				$starttime =  strtotime($method['dateday']) -1;
				$endtime =  (strtotime($method['dateday']) + 86400 -1);
				$map['date'] =  array(array('gt',$starttime),array('lt',$endtime));
				$i = 4;
				foreach($yuyueObj->where($map)->select() as $v){
					$j = 1;
					$worksheet->Cells($i,$j++)->Value = $v['date'] = date('Y/m/d',$v['date']);
					$worksheet->Cells($i,$j++)->Value = $v['binzhong'] = mb_convert_encoding($binzhong[$v['binzhong']],"gb2312","UTF-8");
					$worksheet->Cells($i,$j++)->Value = $v['uid'] = mb_convert_encoding($user[$v['uid']]['name'], "gb2312","UTF-8");
					$worksheet->Cells($i,$j++)->Value = $v['yuyueid'];
					$worksheet->Cells($i,$j++)->Value = mb_convert_encoding($v['name'],"gb2312","UTF-8"); //姓名
					$worksheet->Cells($i,$j++)->Value = mb_convert_encoding($v['proficient'],"gb2312","UTF-8"); //专家
					$worksheet->Cells($i,$j++)->Value = $v['Medicalnumber']; //就诊号
					$worksheet->Cells($i,$j++)->Value = mb_convert_encoding($v['comment'],"gb2312","UTF-8"); //备注
					$i++;
				}
				$worksheet->SaveAs($file);
				$app->WorkBooks->Close();
				unset($app);
				$this->success('发送成功',$urlpath.$pathname);
			break;
			case 'one':
				$app->WorkBooks->Open($path.'one.xls');
				$worksheet = $app->WorkSheets(1);
				$map['uid'] = $method['uid'];
				$map['date'] = strtotime($method['datemonth']);
				$usermap['did'] = $userinfo['did'];
				foreach($userObj->field('id,nickname as name')->where($usermap)->select() as $v){
					$user[$v['id']] = $v;
				}
				$worksheet->Cells(1,1)->Value = mb_convert_encoding($user[$method['uid']]['name'].' 预约情况表（'.date('Y-m',$map['date']).'）', "gb2312","UTF-8");
				$counday = idate('t',$map['date']);
				$s = mktime(0,0,0,date('m',$map['date']),1,date('Y',$map['date'])) -1;
				$e = mktime(24,0,0,date('m',$map['date']),$counday,date('Y',$map['date'])) -1;
				$map['date'] =  array(array('gt',$s),array('lt',$e));
				$i = 3;
				foreach($yuyueObj->where($map)->select() as $v){
					$j = 1;
					$worksheet->Cells($i,$j++)->Value = $v['date'] = date('Y/m/d',$v['date']);
					$worksheet->Cells($i,$j++)->Value = $v['binzhong'] = mb_convert_encoding($binzhong[$v['binzhong']],"gb2312","UTF-8");
					$worksheet->Cells($i,$j++)->Value = $v['uid'] = mb_convert_encoding($user[$v['uid']]['name'], "gb2312","UTF-8");
					$worksheet->Cells($i,$j++)->Value = $v['yuyueid'];
					$worksheet->Cells($i,$j++)->Value = mb_convert_encoding($v['name'],"gb2312","UTF-8"); //姓名
					$worksheet->Cells($i,$j++)->Value = mb_convert_encoding($v['proficient'],"gb2312","UTF-8"); //专家
					$worksheet->Cells($i,$j++)->Value = date('Y/m/d',$v['starttime']);
					$worksheet->Cells($i,$j++)->Value = date('Y/m/d',$v['endtime']);
					$worksheet->Cells($i,$j++)->Value = $v['Medicalnumber']; //就诊号
					$worksheet->Cells($i,$j++)->Value = date('Y/m/d',$v['Clinicaltime']); //
					$worksheet->Cells($i,$j++)->Value = mb_convert_encoding($v['comment'],"gb2312","UTF-8"); //
					$i++;
				}
				$worksheet->SaveAs($file);
				$app->WorkBooks->Close();
				unset($app);
				$this->success('发送成功',$urlpath.$pathname);
			break;
		}
	}
}